﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;

namespace AllenalaMente.Games.Bubbles
{
    public enum BubbleColor
    {
        Blue,
        Green,
        Red,
        Brown,
        Yellow,
        Cyan,
        Magenta,
        Purple,
        TotalColors
    }

    public partial class Bubble : UserControl
    {
        public Point speed;
        private int rotationSpeed;
        public int radius, value;
        public bool collisionResolved;
        private Random random;
        private RadialGradientBrush fillGradient;
        public EventHandler BubblePopped;
        private List<Ellipse> popBubbles;

        public Bubble(BubbleColor color, int value, int radius, Point speed, bool letters)
        {
            InitializeComponent();

            Canvas.SetLeft(Ellipse, -radius);
            Canvas.SetTop(Ellipse, -radius);
            this.radius = radius;
            this.value = value;

            random = new Random();

            fillGradient = new RadialGradientBrush(Colors.White, ConvertBubbleColor(color));
            fillGradient.MappingMode = BrushMappingMode.RelativeToBoundingBox;
            fillGradient.SpreadMethod = GradientSpreadMethod.Pad;
            fillGradient.GradientOrigin = new Point(0.3, 0.3);
            fillGradient.Opacity = 0.8;

            Ellipse.Fill = fillGradient;

            Ellipse.Width = Ellipse.Height = radius * 2;
            textValue.Width = Ellipse.Width;
            //Canvas.SetTop(textValue, (Ellipse.Width / 10) * 3);
            Canvas.SetLeft(textValue, -radius);
            if (letters)
            {
                textValue.Text = Convert.ToChar(value).ToString();
                textValue.TextDecorations = TextDecorations.Underline;
            }
            else
            {
                textValue.Text = value.ToString();
                if (Math.Abs(value) == 6 || Math.Abs(value) == 9)
                    textValue.TextDecorations = TextDecorations.Underline;
            }

            this.speed = speed;
            transformBubble.Rotation = random.Next(-45, 45);
            rotationSpeed = random.Next() % 2 == 0 ? rotationSpeed = -1 : rotationSpeed = 1;

            collisionResolved = false;
            popBubbles = new List<Ellipse>();
        }


        #region Metodi

        public void UpdatePosition()
        {
            transformBubble.Rotation += rotationSpeed;
            transformBubble.TranslateX += speed.X;
            transformBubble.TranslateY += speed.Y;
        }

        public void Rebound(bool leftright, bool updown)
        {
            if (leftright)
                speed.X = -speed.X;
            if (updown)
                speed.Y = -speed.Y;
            collisionResolved = true;
        }

        private Color ConvertBubbleColor(BubbleColor color)
        {
            Color finalColor = Colors.Transparent;
            switch (color)
            {
                case BubbleColor.Blue:
                    finalColor = Colors.Blue;
                    break;
                case BubbleColor.Green:
                    finalColor = Colors.Green;
                    break;
                case BubbleColor.Red:
                    finalColor = Colors.Red;
                    break;
                case BubbleColor.Brown:
                    finalColor = Colors.Brown;
                    break;
                case BubbleColor.Yellow:
                    finalColor = Colors.Yellow;
                    break;
                case BubbleColor.Cyan:
                    finalColor = Colors.Cyan;
                    break;
                case BubbleColor.Magenta:
                    finalColor = Colors.Magenta;
                    break;
                case BubbleColor.Purple:
                    finalColor = Colors.Purple;
                    break;
            }
            return finalColor;
        }


        public void Pop()
        {
            LayoutRoot.Children.Remove(Ellipse);
            this.transformBubble.Rotation = 0;

            int numPopBubbles = random.Next(2, 5);
            for (int i = 0; i < numPopBubbles; i++)
            {
                Ellipse popBubble = new Ellipse();

                popBubble.Height = random.Next(10, radius);
                popBubble.Width = popBubble.Height;

                popBubble.Fill = fillGradient;
                LayoutRoot.Children.Add(popBubble);
             

                popBubbles.Add(popBubble);

                int popBubbleY = random.Next(-radius, radius);
                Canvas.SetLeft(popBubble, random.Next(-radius, radius));
                Canvas.SetTop(popBubble, popBubbleY);
                Canvas.SetZIndex(popBubble, 10);

                Storyboard popBubbleAnimation = new Storyboard();
                popBubbleAnimation.Duration = TimeSpan.FromSeconds(1);

                DoubleAnimation popBubbleOpacity = new DoubleAnimation();
                popBubbleOpacity.Duration = popBubbleAnimation.Duration;
                popBubbleOpacity.From = 1;
                popBubbleOpacity.To = 0;
                popBubbleAnimation.Children.Add(popBubbleOpacity);
                Storyboard.SetTarget(popBubbleOpacity, popBubble);
                Storyboard.SetTargetProperty(popBubbleOpacity, new PropertyPath("(UIElement.Opacity)"));

                DoubleAnimation popBubbleMovement = new DoubleAnimation();
                popBubbleMovement.Duration = popBubbleAnimation.Duration;
                popBubbleMovement.From = popBubbleY;
                popBubbleMovement.To = popBubbleY + 50;
                popBubbleAnimation.Children.Add(popBubbleMovement);
                Storyboard.SetTarget(popBubbleMovement, popBubble);
                Storyboard.SetTargetProperty(popBubbleMovement, new PropertyPath("(Canvas.Top)"));

                popBubbleAnimation.Completed += new EventHandler(popBubbleAnimation_Completed);
                popBubbleAnimation.Begin();
            }

            textValue.Visibility = Visibility.Collapsed;
        }

        void popBubbleAnimation_Completed(object sender, EventArgs e)
        {
            Storyboard popBubbleAnimation = (Storyboard)sender;
            popBubbleAnimation.Completed -= popBubbleAnimation_Completed;

            popBubbles.Clear();
            LayoutRoot.Children.Clear();

            if (BubblePopped != null)
                BubblePopped(this, null);
        }

        #endregion

    }
}
